/**
 * 
 */

function validateForm() {
	var div = document.getElementById("error");
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var restaurantName = document.forms["addrestaurant"]["restaurantName"].value;
	var openTime = document.forms["addrestaurant"]["openTime"].value;
	var closeTime = document.forms["addrestaurant"]["closeTime"].value;
	var deliveryPrice = document.forms["addrestaurant"]["deliveryPrice"].value;
	var minimumBill = document.forms["addrestaurant"]["minOrder"].value;
	var city = document.forms["addrestaurant"]["city"].value;
	var districtId = document.forms["addrestaurant"]["districtId"].value;
	var check = true;
	var textError = null;
	if (restaurantName == null || restaurantName == "") {
		textError = "Restaurant name must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (closeTime == null || closeTime == "") {
		textError = "Close Time must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (openTime == null || openTime == "") {
		textError = "Open Time must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (deliveryPrice == null || deliveryPrice == "") {
		textError = "Delivery Price must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (minimumBill == null || minimumBill == "") {
		textError = "Minimum Bill must be filled out";
		addElement(div, textError);
		check = false;
	}
	if (city == 0) {
		textError = "Please choose a City!";
		addElement(div, textError);
		check = false;
	} else if (districtId == 0) {
		textError = "Please choose a District!";
		addElement(div, textError);
		check = false;
	}

	if (checkExistedRestaurant() == true) {
		textError = "Restaurant Name has existed!";
		addElement(div, textError);
		check = false;
	}

	return check;
}

function addElement(div, textError) {

	var dv = document.createElement("DIV");
	dv.setAttribute("class", "alert alert-danger");
	var sp1 = document.createElement("SPAN");
	sp1.setAttribute("class", "glyphicon glyphicon-exclamation-sign");
	dv.appendChild(sp1);

	var text = document.createTextNode(textError);

	dv.appendChild(text);
	div.appendChild(dv);
}

function checkExistedRestaurant() {
	var flag = false;
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/core-web-admin/restaurant/checkexistedrestaurant",
		"data" : {
			"restaurantName" : $('#restaurantName').val()
		},
		"success" : function(data) {
			if (data == "true") {
				flag = true;
			}
			if (data == "false") {
				flag = false;
			}
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK EXISTED RESTAURANT NAME!");
			flag = false;
		}
	});
	return flag;
}

function validateUpdateForm() {
	var div = document.getElementById("error");
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var openTime = document.forms["addrestaurant"]["openTime"].value;
	var closeTime = document.forms["addrestaurant"]["closeTime"].value;
	var deliveryPrice = document.forms["addrestaurant"]["deliveryPrice"].value;
	var minimumBill = document.forms["addrestaurant"]["minOrder"].value;

	var check = true;
	var textError = null;

	if (closeTime == null || closeTime == "") {
		textError = "Close Time must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (openTime == null || openTime == "") {
		textError = "Open Time must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (deliveryPrice == null || deliveryPrice == "") {
		textError = "Delivery Price must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (minimumBill == null || minimumBill == "") {
		textError = "Minimum Bill must be filled out";
		addElement(div, textError);
		check = false;
	}

	return check;
}

function showImage() {
	var div = document.getElementById('imgs');
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var reader = new FileReader();
	reader.onload = function(evt) {
		var img = '<img id="image" src="' + evt.target.result + '" />';
		$('#imgs').append(img);
	};
	reader.readAsDataURL(document.getElementById("resImg").files[0]);
}

function addInputImage() {
	var src = document.getElementById('image').getAttribute('src');
	$('#imgs').append(
			'<input type="hidden" id="restaurantImage" name="restaurantImage" value="'
					+ src + '"/>');
}

function getDistrict() {
	var city = $('#city').val();
	var district = document.getElementById('checkboxlist');
	while (district.hasChildNodes()) {
		district.removeChild(district.childNodes[0]);
	}
	if (city == 0) {
		$('#districtId option[value!="0"]').remove();
	} else {
		$('#districtId option[value!="0"]').remove();

		$.ajax({
			"type" : "GET",
			"async" : false,
			"contentType" : "application/json;charset=utf-8",
			"dataType" : "json",
			"url" : "/core-web-admin/restaurant/getdistrictbycityid",
			"data" : {
				"cityId" : $('#city').val(),
			},
			"success" : function(data) {
				$.each(data, function(index, value) {
					$('#districtId').append(
							$('<option>').text(value.districtName).attr(
									'value', value.districtId));
				});
				var div = document.getElementById('checkboxlist');
				$.each(data, function(index, value) {
					addDistrictCheckbox(div, value.districtName, value.districtId);
				});
			},
			"error" : function(data) {
				alert("ERROR WHEN GET DISTRICT!");
				flag = false;
			}
		});
	}
}

function addDistrictCheckbox(div, value, id) {
	var dv = document.createElement("DIV");
	var input = document.createElement("INPUT");
	input.setAttribute("type", "checkbox");
	input.setAttribute("name", "chq");
	input.setAttribute("value", id);
	dv.appendChild(input);
	var text = document.createTextNode(value);
	dv.appendChild(text);
	div.appendChild(dv);
}

function getListDeliveryHome() {
	$.ajax({
		"type" : "GET",
		"async" : false,
		"contentType" : "application/json;charset=utf-8",
		"dataType" : "json",
		"url" : "/core-web-admin/restaurant/getdistrictbycityid",
		"data" : {
			"cityId" : $('#city').val(),
		},
		"success" : function(data) {
			var div = document.getElementById('checkboxlist');
			var deHome = document.getElementById('deHome').value;
			var arrDeHome = deHome.split(",");
			$.each(data, function(index, value) {
				if (checkExistValueInArray(arrDeHome, value.districtId) == true) {
					addDistrictCheckboxhasChecked(div, value.districtName, value.districtId);
				} else {
					addDistrictCheckbox(div, value.districtName, value.districtId);
				}
			});
		},
		"error" : function(data) {
			alert("ERROR WHEN GET DISTRICT!");
			flag = false;
		}
	});
}

function getListCategoryIds() {
	var cate = document.getElementById('cate').value;
	var arrCate = cate.split(",");
	var length = cate.length;
	var i;
	for (i = 0; i < length; i++) {
		$('#checkboxcategory input[value='+ arrCate[i] +']').attr("checked", "checked");
	}
}

function checkExistValueInArray(arr, value) {
	var flag = false;
	var i;
	var length = arr.length;
	for (i = 0; i < length; i++) {
		if (arr[i] == value) {
			return true;
		}
	}
	return flag;
}

function addDistrictCheckboxhasChecked(div, value, id) {
	var dv = document.createElement("DIV");
	var input = document.createElement("INPUT");
	input.setAttribute("type", "checkbox");
	input.setAttribute("name", "chq");
	input.setAttribute("value", id);
	input.setAttribute("checked", "checked");
	dv.appendChild(input);
	var text = document.createTextNode(value);
	dv.appendChild(text);
	div.appendChild(dv);
}

function getDeliveryHome() {
	var chkArray = [];
	$('#checkboxlist input:checked').each(function() {
		chkArray.push($(this).val());
	});
	var selected = chkArray.join(',');
	$('#checkboxlist').append('<input type="hidden" id="deliveryHome" name="deliveryHome" value="' + selected + '" />');
}

function getCategoryIds() {
	var cateArray = [];
	$('#checkboxcategory input:checked').each(function() {
		cateArray.push($(this).val());
	});
	var valueSelected = cateArray.join(',');
	$('#checkboxcategory').append('<input type="hidden" id="categoryIds" name="categoryIds" value="' + valueSelected + '" />');
}

function backPage() {
	history.back();
}

